//
//  SearchResultVC.swift
//  Employee
//
//  Created by swordjoy on 2017/4/18.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class SearchResultVC: BaseVC {

  var selectModel: ((ChatUser) -> ())?
  
  var models: [ChatUser] = [] {
    didSet {
      tableView.reloadData()
    }
  }
  
  fileprivate var tableView: UITableView!
  
  override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = GlobalConfig.BGWhiteColor
    
    create()
    
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    GlobalConfig.baiduStatisticsPageViewStart("通讯录搜索")
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    
    GlobalConfig.baiduStatisticsPageViewEnd("通讯录搜索")
  }


}

// MARK: - fileprivate

extension SearchResultVC {
  
  fileprivate func create() {
    
    tableView = UITableView()
    tableView.backgroundColor = UIColor.clear
    tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height - 44)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
    tableView.rowHeight = SelectEmployeeCell.height
    tableView.register(AddressBookCell.self, forCellReuseIdentifier: AddressBookCell.identifier)
    view.addSubview(tableView)

  }
  
}

// MARK: - UITableViewDataSource

extension SearchResultVC : UITableViewDataSource {
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return models.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: AddressBookCell.identifier) as! AddressBookCell
    cell.chatUser = models[indexPath.row]
    return cell
    
  }
  
}

// MARK: - UITableViewDelegate

extension SearchResultVC : UITableViewDelegate {
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if let closure = selectModel {
      closure(models[indexPath.row])
    }
    
//    let vc = ChatUserInfoVC()
//    vc.model = models[indexPath.row]
//    vc.type = .address
//    
//    parentVC.navigationController?.pushViewController(vc, animated: true)
//    
//    tableView.deselectRow(at: indexPath, animated: true)
    
  }
  
}
